package com.yunhe.policyfulfillment.authority.controller;

import com.yunhe.common.exception.UniqueConstraintsException;
import com.yunhe.common.model.ResultObject;
import com.yunhe.policyfulfillment.authority.domain.PolicyMenu;
import com.yunhe.policyfulfillment.authority.service.PolicyMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhengbo
 * @date 2021/9/19 15:28
 */
@RestController
@RequestMapping(value = "/policyMenu")
@Api(value = "policyMenu", description = "菜单", tags = {"Model_PolicyMenu"})
public class PolicyMenuController {

    @Autowired
    private PolicyMenuService policyMenuService;

    @ApiOperation("新增菜单")
    @PostMapping("/createPolicyMenu")
    public ResponseEntity<ResultObject> createPolicyMenu(@RequestBody PolicyMenu policyMenu) throws UniqueConstraintsException {
        policyMenuService.createMenuSelective(policyMenu);
        return new ResponseEntity<>(new ResultObject<>(), HttpStatus.OK);
    }

    @ApiOperation("更新菜单")
    @PostMapping("/updatePolicyMenu")
    public ResponseEntity<ResultObject> updatePolicyMenu(@RequestBody PolicyMenu policyMenu) throws UniqueConstraintsException {
        policyMenuService.updateMenuByPrimaryKeySelective(policyMenu);
        return new ResponseEntity<>(new ResultObject<>(), HttpStatus.OK);
    }

    @ApiOperation("删除菜单")
    @PostMapping("/deletePolicyMenu")
    public ResponseEntity<ResultObject> deletePolicyMenu(@RequestParam(value = "ids") String ids) {
        policyMenuService.deleteMenuByIds(ids);
        return new ResponseEntity<>(new ResultObject<>(), HttpStatus.OK);
    }

    @ApiOperation("获取所有菜单列表")
    @GetMapping("/searchPolicyMenuList")
    public ResponseEntity<ResultObject> searchPolicyMenuList() {
        List<PolicyMenu> policyMenuList = policyMenuService.searchPolicyMenus();
        return new ResponseEntity<>(new ResultObject<>(policyMenuList), HttpStatus.OK);
    }

    @ApiOperation("根据角色id获取菜单列表(绑定解绑时用)")
    @GetMapping("/searchPolicyMenuListByRoleId")
    public ResponseEntity<ResultObject> searchPolicyMenuListByRoleId(@RequestParam(value = "roleId") Long roleId) {
        List<PolicyMenu> policyMenuList = policyMenuService.searchPolicyMenusByRoleId(roleId);
        return new ResponseEntity<>(new ResultObject<>(policyMenuList), HttpStatus.OK);
    }

    @ApiOperation("根据角色id获取菜单列表(登录时用)")
    @GetMapping("/searchPolicyMenuListByRoleIdForLogin")
    public ResponseEntity<ResultObject> searchPolicyMenuListByRoleIdForLogin(@RequestParam(value = "roleId") Long roleId) {
        List<PolicyMenu> policyMenuList = policyMenuService.searchPolicyMenuListByRoleIdForLogin(roleId);
        return new ResponseEntity<>(new ResultObject<>(policyMenuList), HttpStatus.OK);
    }
}
